home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / printing / printdialogmagic / printdialogmagic.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  4.3 KB  |  156 lines

  1. /*
  2.     File:        PrintDialogMagic.c
  3.  
  4.     Contains:    PrintDialogMagic.c -- an example of how to print from an application
  5.                 while still calling the print dialog (so that LW8.3 and such do the
  6.                 right thing with the print record), but not requiring the user to
  7.                 actually press any buttons or anything. This is most useful for apps
  8.                 that want to do automated printing, such as a log to printer option
  9.                 within some sort of server application.
  10.  
  11.     Written by: Dave Polaschek    
  12.  
  13.     Copyright:    Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
  14.  
  15.                 You may incorporate this Apple sample source code into your program(s) without
  16.                 restriction. This Apple sample source code has been provided "AS IS" and the
  17.                 responsibility for its operation is yours. You are not permitted to redistribute
  18.                 this Apple sample source code as "Apple sample source code" after having made
  19.                 changes. If you're going to re-distribute the source, we require that you make
  20.                 it clear in the source that the code was descended from Apple sample source
  21.                 code, but that you've made changes.
  22.  
  23.     Change History (most recent first):
  24.                 7/26/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  25.                 
  26.  
  27. */
  28.  
  29. #include <Printing.h>
  30. #include <MixedMode.h>
  31. #include <SegLoad.h>
  32. #include <Resources.h>
  33. #include <ToolUtils.h>
  34. #include <Fonts.h>
  35.  
  36. /* Declare ‘pascal’ functions and procedures */
  37.  
  38. pascal    TPPrDlg MyJobDlgInit(THPrint);        /* Our extention to PrJobInit.        */
  39. OSErr    Print(void);
  40. pascal    Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  41.  
  42. static TPPrDlg PrtJobDialog;        /* pointer to job dialog */
  43.  
  44. static ModalFilterUPP    myFilterUPP;
  45.  
  46. /*------------------------------------------------------------------------*/
  47.  
  48. OSErr Print(void)
  49. {
  50.     TPPrPort    pPrPort;
  51.     Rect        aRect;
  52.     TPrStatus    theStatus;
  53.     THPrint        hPrintRec;
  54.     
  55.     
  56.     /* call PrJobInit to get pointer to the invisible job dialog */
  57.     hPrintRec = (THPrint)(NewHandle(sizeof(TPrint)));
  58.     PrintDefault(hPrintRec);
  59.     PrValidate(hPrintRec);
  60.     if (PrError() != noErr)
  61.         return PrError();        
  62.  
  63.     PrtJobDialog = PrJobInit(hPrintRec);
  64.        
  65.     if (PrError() != noErr)
  66.         return PrError();        
  67.  
  68.     if (!PrDlgMain(hPrintRec, NewPDlgInitProc(MyJobDlgInit))) /* this line does all the stuff */
  69.         return iPrAbort;
  70.  
  71.     if (PrError() != noErr)
  72.         return PrError();        
  73.  
  74.     pPrPort = PrOpenDoc(hPrintRec, NULL, NULL);
  75.     PrOpenPage(pPrPort, NULL);
  76.     
  77.     aRect = (*hPrintRec)->prInfo.rPage;
  78.     InsetRect(&aRect, 20, 20);
  79.     PenSize(4, 4);
  80.     FrameRect(&aRect);
  81.     
  82.     PrClosePage(pPrPort);
  83.     PrCloseDoc(pPrPort);
  84.  
  85.     if (!PrError() && (*hPrintRec)->prJob.bJDocLoop == bSpoolLoop)
  86.         PrPicFile(hPrintRec, NULL, NULL, NULL, &theStatus);
  87.  
  88.  
  89. /* that's all for now */
  90.         
  91.     if (hPrintRec) DisposeHandle((Handle) hPrintRec);
  92.     
  93.     return(noErr);
  94. } /* Print */
  95.  
  96. /*------------------------------------------------------------------------*/
  97.  
  98. pascal TPPrDlg MyJobDlgInit(THPrint hPrint)
  99. /*
  100.    this routine appends items to the standard job dialog and sets up the
  101.    user fields of the printing dialog record TPRDlg
  102.  
  103.    This routine will be called by PrDlgMain
  104. */
  105. {
  106. #pragma unused(hPrint)
  107.     /* we're going to be simple-minded about this, and just patch the
  108.     ** modal-filter proc for the dialog, and have it say that the user
  109.     ** actually hit the enter-key
  110.     
  111.     ** a more elaborate scheme would hide the dialog, patch ShowWindow
  112.     ** and/or ShowHide and patch out ModalDialog so that the dialog
  113.     ** never even appeared.
  114.     
  115.     ** Our method will briefly show the dialog, which will immediately
  116.     ** go away, which will blink the screen a little. Bummer.
  117.     */
  118.     
  119.     PrtJobDialog->pFltrProc = myFilterUPP;
  120.     
  121.     return PrtJobDialog;
  122. } /*myJobDlgInit*/
  123.  
  124. pascal    Boolean    myFilter(DialogPtr theDialog, EventRecord *theEvent, short *itemHit)
  125. {
  126. #pragma unused(theDialog)
  127. #pragma unused(theEvent)
  128.     *itemHit = 1;
  129.     return(true);
  130. }
  131.  
  132.  
  133. void main(void)
  134. {    
  135.     Rect        myWRect;
  136.     OSErr        err;
  137.     
  138.     InitGraf(&qd.thePort);
  139.     InitFonts();
  140.     InitWindows();
  141.     InitMenus();
  142.     InitDialogs((long)nil);
  143.     InitCursor();
  144.     SetRect(&myWRect,50,260,350,340);
  145.     
  146.     /* call the routine that does printing */
  147.     PrOpen();
  148.     /* build my UPP */
  149.     myFilterUPP = NewModalFilterProc(myFilter);
  150.     err = Print();
  151.     /* clean up */
  152.     DisposeRoutineDescriptor(myFilterUPP);
  153.     
  154.     PrClose();
  155. } /* main */
  156.